#
#        Python GUI - Events - Swing
#

#TODO: global_position

from GEvents import Event as GEvent

from java.awt.event import KeyEvent,MouseEvent

import time

_j_event_type_to_kind = {
    KeyEvent.KEY_PRESSED: 'key_down',
    KeyEvent.KEY_RELEASED: 'key_up',
    MouseEvent.MOUSE_PRESSED: 'mouse_down',
    MouseEvent.MOUSE_RELEASED: 'mouse_up',
}

_j_keycode_to_keyname = {
    KeyEvent.VK_UP: 'up_arrow',
    KeyEvent.VK_DOWN: 'down_arrow',
    KeyEvent.VK_LEFT: 'left_arrow',
    KeyEvent.VK_RIGHT: 'right_arrow',
    KeyEvent.VK_F1: 'f1',
    KeyEvent.VK_F2: 'f2',
    KeyEvent.VK_F3: 'f3',
    KeyEvent.VK_F4: 'f4',
    KeyEvent.VK_F5: 'f5',
    KeyEvent.VK_F6: 'f6',
    KeyEvent.VK_F7: 'f7',
    KeyEvent.VK_F8: 'f8',
    KeyEvent.VK_F9: 'f9',
    KeyEvent.VK_F10: 'f10', 
    KeyEvent.VK_F11: 'f11',
    KeyEvent.VK_F12: 'f12',
    KeyEvent.VK_F13: 'f13',
    KeyEvent.VK_F14: 'f14',
    KeyEvent.VK_F15: 'f15',
    KeyEvent.VK_DELETE: 'delete',
    KeyEvent.VK_HOME: 'home',
    KeyEvent.VK_END: 'end',
    KeyEvent.VK_PAGE_UP: 'page_up',
    KeyEvent.VK_PAGE_DOWN: 'page_down',
    KeyEvent.VK_CLEAR: 'clear',
    KeyEvent.VK_HELP: 'help',
}

_j_keycodes_as_ascii="abcdefghijklmnopqrstuvwxyz0123456789\b\t\n\r`-=[]\;',./"

def _j_get_keyname(keycode):
    try:
        try:
            x=chr(keycode)
        except ValueError:
            x=_j_keycode_to_keyname[keycode]
        else:
            if x not in _j_keycodes_as_ascii:
                x=_j_keycode_to_keyname[keycode]
    except KeyError:
        x='<unknown keycode: %d>'%keycode
    return x
    

class Event(GEvent):
    
    def __init__(self,_j_event):
        self._j_event=_j_event
                
        self.kind=_j_event_type_to_kind[_j_event.getID()]
        
        self.global_position=None
        
        if self.kind in ['mouse_down', 'mouse_up']:
            self.position=(_j_event.getX(),_j_event.getY())
        else:
            self.position=None
        
        self.time=self._j_event.getWhen()
        
        if self.kind in ['mouse_down']:
            self.num_clicks=_j_event.getClickCount()
        else:
            self.num_clicks=None
        
        if self.kind in ['key_down','key_up']:
            self.key=_j_get_keyname(_j_event.getKeyCode())
            self.char=_j_event.getKeyChar()
            if self.char==KeyEvent.CHAR_UNDEFINED:
                self.char=None
            self.auto=None
        else:
            self.key=None
            self.char=None
            self.auto=None
        
        self.shift=_j_event.isShiftDown()
        self.control=_j_event.isControlDown()
        self.option=_j_event.isAltDown()
        
        self.extend_contig=self.shift
        self.extend_noncontig=_j_event.isMetaDown()
        
    def _platform_modifiers_str(self):
        return ""
        
        
        
